package Multithread.Thread;


import java.util.concurrent.*;

public class ThreadPool {

    public static void main(String[] args) throws InterruptedException {

//        1.创建线程池
        fun01();
    }

    /**
     * 1.创建线程池
     */
    public static void fun01() throws InterruptedException {

//        1.创建线程池对象
        ExecutorService executorService_01 = Executors.newCachedThreadPool(); //没有上限的线程池
//        ExecutorService executorService_01 = Executors.newFixedThreadPool(10); //有数量上限的线程池

//        2.提交任务
        executorService_01.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("new Thread 01");
            }
        });
        Thread.sleep(1000);
        executorService_01.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("new Thread 02");
            }
        });
        Thread.sleep(1000);
        executorService_01.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("new Thread 03");
            }
        });

//        3.销毁线程池
        executorService_01.shutdown();

    }

    /**
     * 2.创建更灵活的线程池
     */
    public static void fun02() {

//        1.创建ThreadPoolExecutor线程池
//        参数一：核心线程数量
//        参数二：最大线程数量
//        参数三：空闲线程最大存活时间
//        参数四：时间单位
//        参数五：任务队列
//        参数六：创建线程工厂
//        参数七：任务的拒绝策略
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                6,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
                );
    }

}
